//登陆页面  输入框，登陆按钮，保存到本地的账号等
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test_xzkdemo/contanst/userCotanst.dart';
import 'package:test_xzkdemo/utils/common/global.dart';

//列表页面示例
class LoginPage extends StatefulWidget {
//  MyListPage({Key key}) : super(key: key);

  @override
  _LoginPageState createState() => new _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  // Key key = GlobalKey();

  String _userName;
  String _userPwd;

  /*
   * 利用SharedPreferences存储数据
   */
  Future saveUserMsg(String username, String userPwd) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setString(UserContanst.userName, username);
    sharedPreferences.setString(UserContanst.userPwd, userPwd);
    sharedPreferences.setBool(UserContanst.userLoginstatus, true);
  }

  /*
   * 获取存在SharedPreferences中的数据
   */
  Future getUserMsg() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    setState(() {
      _userName = sharedPreferences.get(UserContanst.userName);
      _userPwd = sharedPreferences.get(UserContanst.userPwd);
    });
  }

  //手机号的控制器
  TextEditingController userController = TextEditingController();

  //密码的控制器
  TextEditingController passController = TextEditingController();

  @override
  void initState() {
    super.initState();
    //获取保存的账号密码
    getUserMsg();
  }

  @override
  Widget build(BuildContext context) {
    G.showToastBottom("展开登陆页");

    userController.text = _userName;
    passController.text = _userPwd;
    print("登陆的$_userName");
    print(_userPwd);

    num buttonWidth = G.screenWidth(context) * 4 / 5;
    return new Scaffold(
      appBar: AppBar(
        title: new Text("登陆"),
        automaticallyImplyLeading: false,
      ),
      body: Column(
        children: <Widget>[
          TextField(
            maxLines: 1,
            controller: userController,
            decoration: InputDecoration(
              labelText: '请输入用户名',
////设置输入框前面有一个电话的按钮 suffixIcon
              prefixIcon: Icon(Icons.person),
              labelStyle: TextStyle(
                fontSize: 14,
                color: Colors.grey,
              ),
            ),
          ),
          TextField(
            maxLines: 1,
            obscureText: true,
            controller: passController,
            decoration: InputDecoration(
              labelText: '请输入密码',
////设置输入框前面有一个电话的按钮 suffixIcon
              prefixIcon: Icon(Icons.lock),
              labelStyle: TextStyle(
                fontSize: 14,
                color: Colors.grey,
              ),
            ),
          ),
          MaterialButton(
            minWidth: buttonWidth,
            textColor: Colors.white,
            color: Colors.blue,
            child: Text("登录"),
            onPressed: () => _login(context),
          ),
        ],
      ),
    );
  }

  void _login(BuildContext context) {
    if (userController.text.length < 1) {
      G.showToastBottom("请输入用户名");
      return;
    }
    if (passController.text.length < 1) {
      G.showToastBottom("请输入密码");
      return;
    }
    saveUserMsg(userController.text, passController.text);
    G.showToastBottom("登陆成功");
    //todo 进入首页
    Navigator.pushReplacementNamed(context, "/home");
  }
}
